/** 
	 * Kunoy 
	 * 合并单元格 
	 * @param {} grid  要合并单元格的grid对象 
	 * @param {} cols  要合并哪几列 [1,2,4] 
	 */  
define(["jquery"], function($) {

	'use strict';	
	var mergeMethod = {
		mergeCells : function(grid,cols){
	
		 	if(grid!=null){
			    var arrayTr=document.getElementById(grid.getId()+"-body").firstChild.firstChild.getElementsByTagName('tr');    
			    var trCount = arrayTr.length;  
			    var arrayTd;  
			    var td;  
			    var merge = function(rowspanObj,removeObjs){ //定义合并函数  
			        if(rowspanObj.rowspan != 1){  
			            arrayTd =arrayTr[rowspanObj.tr].getElementsByTagName("td"); //合并行  
			            td=arrayTd[rowspanObj.td-1];  
			            td.rowSpan=rowspanObj.rowspan;  
			            td.vAlign="middle"; 
			            
			            var heightDefault = $(arrayTd[0]).innerHeight();
			            var height = $(td).innerHeight();  
			            if(height > heightDefault)  
			            {  
			                $(td).css("padding-top",height/2 - heightDefault/2);  
			           
			            } 
			            Ext.each(removeObjs,function(obj){ //隐身被合并的单元格  
			                arrayTd =arrayTr[obj.tr].getElementsByTagName("td");  
			                arrayTd[obj.td-1].style.display='none';                           
			            });  
			        }     
			    };    
			    var rowspanObj = {}; //要进行跨列操作的td对象{tr:1,td:2,rowspan:5}      
			    var removeObjs = []; //要进行删除的td对象[{tr:2,td:2},{tr:3,td:2}]  
			    var col;  
			    Ext.each(cols,function(colIndex){ //逐列去操作tr  
			        var rowspan = 1;  
			        var divHtml = null;//单元格内的数值          
			        for(var i=0;i<trCount;i++){  //i=0表示表头等没用的行  
			            arrayTd = arrayTr[i].getElementsByTagName("td");  
			            var cold=0;  //跳过序号列
			//          Ext.each(arrayTd,function(Td){ //获取RowNumber列和check列  
			//              if(Td.getAttribute("class").indexOf("x-grid-cell-special") != -1)  
			//                  cold++;                               
			//          });  
			            col=colIndex+cold;//跳过RowNumber列和check列  
			            if(!divHtml){  
			                divHtml = arrayTd[col-1].innerHTML;  
			                rowspanObj = {tr:i,td:col,rowspan:rowspan}  
			            }else{  
			                var cellText = arrayTd[col-1].innerHTML;  
			                var addf=function(){   
			                    rowspanObj["rowspan"] = rowspanObj["rowspan"]+1;  
			                    removeObjs.push({tr:i,td:col});  
			                    if(i==trCount-1)  
			                        merge(rowspanObj,removeObjs);//执行合并函数  
			                };  
			                var mergef=function(){  
			                    merge(rowspanObj,removeObjs);//执行合并函数  
			                    divHtml = cellText;  
			                    rowspanObj = {tr:i,td:col,rowspan:rowspan}  
			                    removeObjs = [];  
			                };  
			                if(cellText == divHtml){  
			                    if(colIndex!=cols[0]){   
			                        var leftDisplay=arrayTd[col-2].style.display;//判断左边单元格值是否已display  
			                        if(leftDisplay=='none')  
			                            addf();   
			                        else  
			                            mergef();                             
			                    }else  
			                        addf();                                           
			                }else  
			                    mergef();             
			            }  
			        }  
			    });   
			}
		}
	},
	
	//合并单元格
	mergeCells2: function( grid , cols )  
	{    
	    
	    var arrayTr = document.getElementById(grid.getId()+"-body").firstChild.firstChild.lastChild.getElementsByTagName('tr');  
	     
	    var trCount = arrayTr.length;  //<tr>总行数  
	    var arrayTd;    
	    var td;    
	  
	    //==>显示层将目标格的样式改为.display='none';        
	    var merge = function( rowspanObj , removeObjs )//定义合并函数    
	    {   
	        if( 0 != rowspanObj.rowspan )  
	        {    
	            arrayTd = arrayTr[ rowspanObj.tr ].getElementsByTagName("td"); //合并行    
	            td = arrayTd[rowspanObj.td-1];    
	            td.rowSpan = rowspanObj.rowspan;    
	            td.vAlign = "middle";       
	        
	            var heightDefault = $(arrayTd[0]).innerHeight();
	            var height = $(td).innerHeight();  
	            if(height > heightDefault)  
	            {  
	                $(td).css("padding-top",height/2 - heightDefault/2);  
	           
	            }   
	              
	            
	            Ext.each(removeObjs,function(obj)  
	            {  
	                arrayTd = arrayTr[obj.tr].getElementsByTagName("td");    
	                arrayTd[obj.td-1].style.display='none';    
	                arrayTd[obj.td-1].style.borderTop = "none";    
	            });   
	  
	        }       
	    };      
	   
	  
	    var rowspanObj = {};       
	    var removeObjs = [];  
	    var col;    
	    
	      
	    try  
	    {  
	          
	      
	    Ext.each( cols , function( colIndex )  
	    {   
	        var rowspan = 1;    
	        var divHtml = null;//单元格内的数值            
	        for( var i=0 ; i < trCount ; i++)//==>从第一行数据0开始  
	        {    
	            //==>一个arrayTr[i]是一整行的所有数据, 一个arrayTd是 <td xxxx ><div>具体数值</div></td> ,  
	            arrayTd = arrayTr[i].getElementsByTagName("td");    
	            var cold=0;    
	            
	            col = colIndex + cold;//跳过RowNumber列和check列    
	             
	            if( !divHtml )  
	            {    
	                divHtml = arrayTd[col-1].innerHTML;    
	                divHtml = $(divHtml).text(); //==>拿到真正数值,相比Ext4.1多了一层<div>  
	                rowspanObj = { tr:i,td:col,rowspan:rowspan }    
	            }  
	            else  
	            {    
	                var cellText = arrayTd[col-1].innerHTML;    
	                cellText = $(cellText).text();//==>拿到真正数值   
	                 
	                 
	               var addf = function()  
	                {     
	                    rowspanObj["rowspan"] = rowspanObj["rowspan"]+1;    
	                    removeObjs.push({ tr:i,td:col });    
	                    if( i == trCount-1)    
	                    {     
	                        merge(rowspanObj,removeObjs);//执行合并函数    
	                    }  
	                };    
	                var mergef = function()  
	                {    
	                    merge(rowspanObj,removeObjs);//执行合并函数    
	                    divHtml = cellText;    
	                    rowspanObj = {tr:i,td:col,rowspan:rowspan}    
	                    removeObjs = [];    
	                };    
	                 
	                if( cellText == divHtml )  
	                {    
	                        if( colIndex != cols[0] )  
	                        {     
	                                var leftDisplay = arrayTd[col-2].style.display;//判断左边单元格值是否已display    
	                                if( leftDisplay == 'none' )    
	                                {      
	                                    addf();     
	                                }  
	                                else    
	                                {     
	                                    mergef();             
	                                }                    
	                            }  
	                        else    
	                        {     
	                                addf();            
	                        }                                   
	                }  
	                else    
	                {     
	                    mergef();           
	                }      
	            }    
	        }    
	    });    
	    }catch(e)  
	    {  
	        console.log(e.message)  
	    }  
	}
	
	return mergeMethod;
})